<?php


namespace app\admin\logic;

use think\facade\Cache;
use think\facade\Db;

/**
 *
 * app缓存观察实现类
 * Class AppLogic
 * @package app\admin\cache
 * @author Mr.Li
 * @create 2021-08-18 10:43
 */

class AppLogic implements ObService
{

    const appConfigName = 'app_config_';

    /**
     * 设置app缓存
     * @author Mr.Li
     * @create 2021-08-18 10:24
     */
    public function notify($params){
        $cacheData = [];
        if(isset($params['name'])){
            foreach($params['name'] as $v){
                $cacheData[$v] = $params[$v] ?? '';
            }
        }
        $cacheData &&  Cache::set(self::appConfigName.$params['group'], $cacheData);
    }

    /**
     * 获取缓存
     * @author Mr.Li
     * @create 2021-08-18 11:02
     */
    public static function get($appType){

        $appConfig = Cache::get(self::appConfigName.$appType);
        if(!$appConfig){
            $appGroup = [
                'android' => 'admin_android_app_set',
                'ios' => 'admin_ios_app_set',
            ];
            $appType = $appGroup[$appType] ?? 0;
            $appType && $appConfig = Db::name('admin_setting')->where('group',$appType)->field('name,value')->select()->toArray();
            $appConfig = $appConfig ? array_column($appConfig,'value','name') : [];
        }
        return $appConfig;
    }
}
